package ro.upt.ac.cstaicu.ui;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * This class reads the arguments and the options from the command line and
 * stores them.
 * 
 * @author Cristian-Alexandru STAICU
 * 
 */
public class CommandLineParser {

	/**
	 * the options received from the command line. The key is the option name
	 * and the value is the argument of the option. The "-" is stripped from the
	 * option name. No-arguments options are not allowed.
	 */
	private HashMap<String, String> options = new HashMap<String, String>();
	/**
	 * list of the arguments received from the command line.
	 */
	private ArrayList<String> arguments = new ArrayList<String>();

	/**
	 * Reads the options and the arguments and populates it's intern data
	 * structures.
	 * 
	 * @param args
	 *            a list of raw command line input
	 */
	public CommandLineParser(String args[]) {
		String opt = "";
		int i = 0;
		while (i < args.length) {
			if (args[i].contains("-")) {
				opt = args[i].replaceAll("-", "");
				options.put(opt, args[i + 1]);
				i += 2;
			} else {
				arguments.add(args[i]);
				i++;
			}
		}
	}

	/**
	 * Prints the list of the options received from the command line.
	 */
	public void printOptions() {
		for (String option : options.keySet())
			System.out.println(option);
	}

	/**
	 * The allowed options are matching the following pattern
	 * "-optionName argument". This function returns the argument of a given
	 * option. On fail it returns null.
	 * 
	 * @param opt
	 *            - the option introduced from the command line that you are
	 *            interested in
	 * @return argument of the given option
	 */
	public String getArgument(String opt) {
		if (opt.contains(opt))
			return options.get(opt).toString();
		else
			return null;
	}

	/**
	 * Returns the list of arguments that are given in the command line.
	 * 
	 * @return a list of arguments
	 */
	public ArrayList<String> getArguments() {
		return arguments;
	}
}
